﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Expression.Blend.DataStore.DataStore;
using System.Windows.Media.Animation;
using System.ComponentModel;

namespace MeasurementTools
{

	/// <summary>
	/// Interaction logic for UserControl1.xaml
	/// </summary>
    /// 


	public partial class UserControl1 : UserControl
	{
        int m_controlID;
		Point m_currentMousePosition;
        Point m_circleSize = new Point(150, 150);
        PropertyItem m_piSize = new PropertyItem();
        PropertyItem m_piPosition = new PropertyItem();
        int m_displayFontSizeType = 0;
		
        public SettingItem MSettingItem = new SettingItem();

        DataStore m_dataStore;

        bool m_isMouseDownOnHandleArea = false;
        bool m_isMouseDownOnCircleArea = false;
        bool m_isMouseDownOnPropertyArea = false;
        bool m_isThisControlMoved = false;
        bool m_isPropertyGridMoved = false;
        bool m_isThisControlSelected = false;
        bool m_isShowDetailProperties = false;
        SolidColorBrush m_brushCircleBorder;
        SolidColorBrush m_brushSelected;

        UNIT_TYPE m_calibrationType;
        double m_calibrationValue;

        PositionPreview m_positionPreview = new PositionPreview();

        public DelegateUpdatePositionOfAllPropertyWindow m_delegateUpdatePositionOfAllPropertyWindow = null;

        public event PropertyChangedEventHandler PropertyChanged;

		public UserControl1()
		{
			this.InitializeComponent();

            m_dataStore = this.FindResource("DataStore") as DataStore;
            m_brushCircleBorder = FindResource("BrushCircleBorder") as SolidColorBrush;
            m_brushSelected = FindResource("BrushSelected") as SolidColorBrush;

            main_grid.SizeChanged += new SizeChangedEventHandler(main_grid_SizeChanged);

            grip_preview.Children.Add(m_positionPreview);
            m_positionPreview.Visibility = Visibility.Hidden;

            main_grid.Children.Add(MSettingItem);
            MSettingItem.Visibility = Visibility.Hidden;


            initPropertyWindow();
        }

        public void SetPositionPreviewEnable(bool flag)
        {
            m_positionPreview.IsEnabled = flag;

            if (flag == false)
            {
                m_positionPreview.Visibility = Visibility.Hidden;
            }
            else
            {
                if (IsSelected == true)
                {
                    m_positionPreview.Visibility = Visibility.Visible;
                }
            }
        }

        public int ID
        {
            get { return m_controlID; }
            set { m_controlID = value; }
        }

        public Thickness LinkLineMargin
        {
            get { return property_grid.Margin; }
            set
            {
//                 line_insideCircle.Margin = value;
//                 linemask_insideCircle.Margin = value;
//                 line_insideCircle.UpdateLayout();
//                 linemask_insideCircle.UpdateLayout();
            }
        }

        public void SetCalibration(UNIT_TYPE type, double value)
        {
            m_calibrationType = type;
            m_calibrationValue = value;
        }

        public Thickness PropertySwitcherMargin
        {
            get { return property_grid.Margin; }
            set
            {
//                 properties_hot_area.Margin = value;
//                 properties_hot_area.UpdateLayout();
            }
        }

        public Thickness PropertyWindowMargin
        {
            get {return property_grid.Margin;}
            set { 
                property_grid.Margin = value;
                property_grid.UpdateLayout();

                Thickness switcherMargin = new Thickness();
                switcherMargin.Left = value.Left + 25 - properties_hot_area.ActualWidth;
                switcherMargin.Top = value.Top+20;
                switcherMargin.Right = main_grid.ActualWidth - switcherMargin.Left - properties_hot_area.ActualWidth;
                switcherMargin.Bottom = main_grid.ActualHeight - switcherMargin.Top - properties_hot_area.ActualHeight;
                properties_hot_area.Margin = switcherMargin;
                properties_hot_area.UpdateLayout();

                updateLinkLinePosition();
            }
        }

        void main_grid_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            updatePreviewWindow();


            updatePropertyItem_Size();
            updatePropertyItem_Position();
        }

        void updatePropertyItem_Size()
        {
            string str = "s:" + mask_circle.ActualWidth.ToString() + ", " + mask_circle.ActualHeight.ToString();
            m_piSize.LabelText = str;
        }

        void updatePropertyItem_Position()
        {
            string str = "p:" + (Canvas.GetLeft(this) - 25).ToString() + ", " + (Canvas.GetTop(this) - 25).ToString();
            m_piPosition.LabelText = str;
        }

        public Point ImageSize
        {
            get { return m_positionPreview.ImageSize; }
            set { m_positionPreview.ImageSize = value; }
        }

        public Point ImagePos
        {
            get { return m_positionPreview.ImagePos; }
            set { m_positionPreview.ImagePos = value; }
        }

        public bool DetailProperties
        {
            get { return m_isShowDetailProperties; }
            set
            {
                m_isShowDetailProperties = value;
                OnPropertyChanged("DetailProperties");
            }
        }

        public void initPropertyWindow()
        {
            property_grid.Children.Add(m_piSize);
            m_piSize.ShowMe = true;
            m_piSize.ToolTip = "Size";
            
            property_grid.Children.Add(m_piPosition);
            m_piPosition.ShowMe = true;
            m_piPosition.ToolTip = "Position";

            PropertyItem pi = new PropertyItem();
            property_grid.Children.Add(pi);
            pi.LabelText = "hello";
            pi.ShowMe = false;

            PropertyItem pi2 = new PropertyItem();
            property_grid.Children.Add(pi2);
            pi2.LabelText = "world";
            pi2.ShowMe = false;

            PropertyItem pi3 = new PropertyItem();
            property_grid.Children.Add(pi3);
            pi3.LabelText = "welcome";
            pi3.ShowMe = false;

            PropertyItem pi4 = new PropertyItem();
            property_grid.Children.Add(pi4);
            pi4.LabelText = "3DIIIIIIIIIIIIIIIIIIIIII";
            pi4.ShowMe = false;

            OnPropertyChanged("DetailProperties");
        }

        public double CircleRadius
        {
            get { return main_grid.ActualHeight - handle_area_2.ActualWidth; }
            set
            {
                double preWidth = main_grid.ActualWidth;
                main_grid.Height = value + 50;
                main_grid.Width = value + 50;
                main_grid.UpdateLayout();
                updatePropertyWindow(preWidth, main_grid.ActualWidth);
                OnPropertyChanged("CircleRadius");
            }
        }

        public bool IsSelected
        {
            get { return m_isThisControlSelected; }
            set
            {
                m_isThisControlSelected = value;
                OnPropertyChanged("IsSelected");
            }
        }

        public SolidColorBrush BrushCircleBorder
        {
            get { return m_brushCircleBorder; }
            set
            {
                m_brushCircleBorder.Color = value.Color;
                OnPropertyChanged("BrushCircleBorder");
            }
        }

        public SolidColorBrush BrushSelected
        {
            get { return m_brushSelected; }
            set
            {
                m_brushSelected.Color = value.Color;
                OnPropertyChanged("BrushSelected");
            }
        }


        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }

            if (name == "IsSelected")
            {
                if (IsSelected)
                {
                    handle_area_1.IsEnabled = true;
                    handle_area_2.IsEnabled = true;
                    handle_area_3.IsEnabled = true;
                    handle_area_4.IsEnabled = true;
                    selecting_rectangle.Visibility = Visibility.Visible;
                    DetailProperties = true;

                    if ( m_positionPreview.IsEnabled == true)
                        m_positionPreview.Visibility = Visibility.Visible;
                }
                else
                {
                    handle_area_1.IsEnabled = false;
                    handle_area_2.IsEnabled = false;
                    handle_area_3.IsEnabled = false;
                    handle_area_4.IsEnabled = false;
                    selecting_rectangle.Visibility = Visibility.Hidden;
                    DetailProperties = false;
                    MSettingItem.Visibility = Visibility.Hidden;

                    if ( m_positionPreview.IsEnabled == true)
                        m_positionPreview.Visibility = Visibility.Hidden;
                    
                    main_menu.Visibility = Visibility.Hidden;
                    var storyboard = this.Resources["MouseLeaveOnCircle"] as Storyboard;
                    mask_circle.BeginStoryboard(storyboard);
                }
            }
            else if (name == "DetailProperties")
            {
                if (m_isShowDetailProperties)
                {
                    double marginTop = 0;
                    foreach (UIElement ue in property_grid.Children)
                    {
                        PropertyItem pi = ue as PropertyItem;
                        if (pi != null)
                        {
                            pi.PreviewMe = true;
                            pi.Margin = new System.Windows.Thickness(0, marginTop, 0, 0);
                            marginTop += pi.ItemHeight - 1;
                        }
                    }
                }
                else
                {
                    double marginTop = 0;
                    foreach (UIElement ue in property_grid.Children)
                    {
                        PropertyItem pi = ue as PropertyItem;
                        if (pi != null)
                        {
                            pi.PreviewMe = false;
                            if (pi.ShowMe == true)
                            {
                                pi.Margin = new System.Windows.Thickness(0, marginTop, 0, 0);
                                marginTop += pi.ItemHeight - 1;
                            }
                        }
                    }
                }
            }
        }

//         public void SetCircleColor(SolidColorBrush color)
//         {
//             SolidColorBrush c = FindResource("CircleBorderBrush") as SolidColorBrush;
//             System.Console.WriteLine("current color {0} {1} {2}", c.Color.R, c.Color.G, c.Color.B);
//             c.Color = Colors.Red;
// 
// //             DataStore ds = this.FindResource("DataStore") as DataStore;
// //             ds.isCircleSelected = false;
// // 
// //             var storyboard = this.Resources["CircleOnMouseLeave"] as Storyboard;
// //             mask_circle.BeginStoryboard(storyboard);
//         }

        public int DisplayFontSize
        {
            get { return m_displayFontSizeType; }
            set 
            { 
                m_displayFontSizeType = value; 

                foreach (UIElement ue in property_grid.Children)
                {
                    PropertyItem pi = ue as PropertyItem;
                    if (pi != null)
                    {
                        pi.FontSizeType = m_displayFontSizeType;
                    }
                }

                OnPropertyChanged("DetailProperties");
            }
        }

		
        private void selecting_rectangle_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
			System.Console.WriteLine("value changed");
			
        	// TODO: Add event handler implementation here.
        }

        private void mask_circle_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (m_isMouseDownOnCircleArea)
            {
                Point newMousePosition = e.GetPosition(this.Parent as Canvas);
                double dx = newMousePosition.X - m_currentMousePosition.X;
                double dy = newMousePosition.Y - m_currentMousePosition.Y;

                double newLeft = Canvas.GetLeft(this);
                double newTop = Canvas.GetTop(this);

                Canvas.SetLeft(this, newLeft + dx);
                Canvas.SetTop(this, newTop + dy);

                m_currentMousePosition = newMousePosition;

                m_isThisControlMoved = true;

                updatePreviewWindow();
                updatePropertyItem_Position();
                updatePropertyItem_Size();
                updateSettings();
            }
        }

        private void mask_circle_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            m_currentMousePosition = e.GetPosition(this.Parent as Canvas);
            m_isMouseDownOnCircleArea = true;
            main_menu.Visibility = Visibility.Hidden;
            mask_circle.CaptureMouse();
        }

        private void mask_circle_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (m_isThisControlMoved == false)
            {
                IsSelected = !IsSelected;
            }
            else
                m_isThisControlMoved = false;

            m_isMouseDownOnCircleArea = false;
            mask_circle.ReleaseMouseCapture();
        }

        private void updatePreviewWindow()
        {
            m_positionPreview.CirclePos = new Point(Canvas.GetLeft(this) + main_grid.ActualWidth / 2.0 - 25,
                                                    Canvas.GetTop(this) + main_grid.ActualHeight / 2.0 - 25);
        }

        private void mask_circle_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (m_isThisControlSelected)
                return;

            var storyboard = this.Resources["MouseEnterOnCircle"] as Storyboard;
            mask_circle.BeginStoryboard(storyboard);
            DetailProperties = true;

            if (m_positionPreview.IsEnabled == true)
            {
                m_positionPreview.Visibility = Visibility.Visible;
                updatePreviewWindow();
            }
        }

        private void mask_circle_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (m_isThisControlSelected)
                return;

            var storyboard = this.Resources["MouseLeaveOnCircle"] as Storyboard;
            mask_circle.BeginStoryboard(storyboard);
            DetailProperties = false;

            if ( m_positionPreview.IsEnabled == true)
                m_positionPreview.Visibility = Visibility.Hidden;
        }

        private void show_properties_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            m_dataStore.isPropertiesShown = !m_dataStore.isPropertiesShown;
        }


        #region HANDLES_AREA

        private void handle_area_1_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            m_currentMousePosition = e.GetPosition(this.Parent as Canvas);
            m_isMouseDownOnHandleArea = true;
            handle_area_1.CaptureMouse();
        }

        private void handle_area_1_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            m_isMouseDownOnHandleArea = false;
            handle_area_1.ReleaseMouseCapture();
        }

        private void handle_area_1_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (m_isMouseDownOnHandleArea)
            {
                Point newMousePosition = e.GetPosition(this.Parent as Canvas);
                m_currentMousePosition = newMousePosition;
                updateMouseMove(1, newMousePosition);
            }
        }

        private void handle_area_2_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            m_currentMousePosition = e.GetPosition(this.Parent as Canvas);
            m_isMouseDownOnHandleArea = true;
            handle_area_2.CaptureMouse();
        }

        private void handle_area_2_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            m_isMouseDownOnHandleArea = false;
            handle_area_2.ReleaseMouseCapture();
        }

        private void handle_area_2_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (m_isMouseDownOnHandleArea)
            {
                Point newMousePosition = e.GetPosition(this.Parent as Canvas);
                m_currentMousePosition = newMousePosition;
                updateMouseMove(2, newMousePosition);
            }
        }

        private void handle_area_3_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            m_currentMousePosition = e.GetPosition(this.Parent as Canvas);
            m_isMouseDownOnHandleArea = true;
            handle_area_3.CaptureMouse();
        }

        private void handle_area_3_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            m_isMouseDownOnHandleArea = false;
            handle_area_3.ReleaseMouseCapture();
        }

        private void handle_area_3_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (m_isMouseDownOnHandleArea)
            {
                Point newMousePosition = e.GetPosition(this.Parent as Canvas);
                m_currentMousePosition = newMousePosition;
                updateMouseMove(3, newMousePosition);
            }
        }

        private void handle_area_4_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            m_currentMousePosition = e.GetPosition(this.Parent as Canvas);
            m_isMouseDownOnHandleArea = true;
            handle_area_4.CaptureMouse();
        }

        private void handle_area_4_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            m_isMouseDownOnHandleArea = false;
            handle_area_4.ReleaseMouseCapture();
        }

        private void handle_area_4_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (m_isMouseDownOnHandleArea)
            {
                Point newMousePosition = e.GetPosition(this.Parent as Canvas);
                m_currentMousePosition = newMousePosition;
                updateMouseMove(4, newMousePosition);
            }
        }

        private void updateMouseMove(int area, Point newMousePosition)
        {
            double offsetWidth, offsetHeight;
            double offset;
            double previousWidth = main_grid.ActualWidth;

            switch (area)
            {
                case 1:
                    offsetWidth = Canvas.GetLeft(this) - newMousePosition.X;
                    offsetHeight = Canvas.GetTop(this) - newMousePosition.Y;
                    break;
                case 2:
                    offsetWidth = newMousePosition.X - Canvas.GetLeft(this) - selecting_rectangle.ActualWidth;
                    offsetHeight = Canvas.GetTop(this) - newMousePosition.Y;
                    break;
                case 3:
                    offsetWidth = newMousePosition.X - Canvas.GetLeft(this) - selecting_rectangle.ActualWidth;
                    offsetHeight = newMousePosition.Y - Canvas.GetTop(this) - selecting_rectangle.ActualHeight;
                    break;
                case 4:
                    offsetWidth = Canvas.GetLeft(this) - newMousePosition.X;
                    offsetHeight = newMousePosition.Y - Canvas.GetTop(this) - selecting_rectangle.ActualHeight;
                    break;
                default:
                    return;
            }

            if (selecting_rectangle.ActualWidth + offsetWidth <= 1 ||
                selecting_rectangle.ActualHeight + offsetHeight <= 1)
                return;


            if (offsetWidth > offsetHeight)
                offset = offsetHeight;
            else
                offset = offsetWidth;

            main_grid.Width += offset;
            main_grid.Height += offset;
            main_grid.UpdateLayout();

            switch (area)
            {
                case 1:
                    Canvas.SetTop(this, Canvas.GetTop(this) - offset);
                    Canvas.SetLeft(this, Canvas.GetLeft(this) - offset);
                    break;
                case 2:
                    Canvas.SetTop(this, Canvas.GetTop(this) - offset);
                    break;
                case 3:
                    break;
                case 4:
                    Canvas.SetLeft(this, Canvas.GetLeft(this) - offset);
                    break;
                default:
                    return;
            }

            double currentWidth = main_grid.ActualWidth;

            updatePropertyWindow(previousWidth, currentWidth);
        }


        private void updatePropertyWindow(double previousMainGridWidth, double currentMainGridWidth)
        {
            Thickness currentMargin_insideLine;


            if (previousMainGridWidth > 0)
            {
                double r = currentMainGridWidth / previousMainGridWidth;
                Thickness origMargin = line_insideCircle.Margin;

                currentMargin_insideLine = new Thickness(origMargin.Left * r, origMargin.Top * r, origMargin.Right * r, origMargin.Bottom * r);
            }
            else
            {
                double radius = currentMainGridWidth/2.0 - 25.0;
                double top = currentMainGridWidth/2.0 - (radius*Math.Sin(45.0/Math.PI)+10);
                currentMargin_insideLine = new Thickness(currentMainGridWidth / 2.0, top, top, currentMainGridWidth / 2.0);
            }

            line_insideCircle.Margin = currentMargin_insideLine;
            linemask_insideCircle.Margin = currentMargin_insideLine;
            line_insideCircle.UpdateLayout();
            linemask_insideCircle.UpdateLayout();


            double properties_left = 0;
            double properties_bottom = 0;
            double properties_top = 0;
            double properties_right = 0;
            //double margin = (main_circle.ActualWidth *0.3535) + main_grid.ActualWidth/2.0;
            if (Math.Abs(line_insideCircle.Margin.Left - main_grid.ActualWidth / 2.0) <= 2)
            {
                properties_left = line_insideCircle.Margin.Left + line_insideCircle.ActualWidth - 10;
            }
            else
            {
                properties_left = line_insideCircle.Margin.Left - 10;
            }

            if (Math.Abs(line_insideCircle.Margin.Bottom - main_grid.ActualHeight/2.0)<=2)
            {
                properties_bottom = line_insideCircle.Margin.Bottom + line_insideCircle.ActualHeight - 10;
            }
            else
            {
                properties_bottom = line_insideCircle.Margin.Bottom - 10;
            }

            properties_top = main_grid.ActualHeight - properties_bottom - 36;

            if (properties_left >= main_grid.ActualWidth-50)
                properties_right = main_grid.ActualWidth - properties_left - 50;
            else
                properties_right = 0;

            properties_hot_area.Margin = new System.Windows.Thickness(properties_left, properties_top,
                                                                      properties_right,
                                                                      properties_bottom);
            properties_hot_area.UpdateLayout();

            property_grid.Margin = new System.Windows.Thickness(properties_left + properties_hot_area.ActualWidth - 48, properties_hot_area.Margin.Top - 20, 0, 0);

            updateSettings();
        }

        #endregion


        private void selecting_rectangle_DataContextChanged(object sender, System.Windows.DependencyPropertyChangedEventArgs e)
        {
//             System.Console.WriteLine("hello");
// 
//             if (m_dataStore1 != null && m_dataStore1.isCircleSelected1)
//                 selecting_rectangle.Visibility = Visibility.Visible;
//             else
//                 selecting_rectangle.Visibility = Visibility.Hidden;
        }


        private void property_grid_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            DetailProperties = true;
        }

        private void property_grid_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
        	// TODO: Add event handler implementation here.

            if ( !IsSelected)
                DetailProperties = false;
        }

        private void property_grid_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            m_currentMousePosition = e.GetPosition(this.Parent as Canvas);
            m_isMouseDownOnPropertyArea = true;
            //property_grid.CaptureMouse();
            //DetailProperties = !DetailProperties;
        }

        private void property_grid_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            m_isMouseDownOnPropertyArea = false;
            property_grid.ReleaseMouseCapture();
            OnPropertyChanged("DetailProperties");
        }



        private void property_grid_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (m_isMouseDownOnPropertyArea)
            {
                property_grid.CaptureMouse();

                Point newMousePosition = e.GetPosition(this.Parent as Canvas);
                double dx = newMousePosition.X - m_currentMousePosition.X;
                double dy = newMousePosition.Y - m_currentMousePosition.Y;

                Thickness newPropertyWindowMargin = property_grid.Margin;
                newPropertyWindowMargin.Left += dx;
                newPropertyWindowMargin.Right -= dx;
                newPropertyWindowMargin.Top += dy;
                newPropertyWindowMargin.Bottom -= dy;

                property_grid.Margin = newPropertyWindowMargin;
                property_grid.UpdateLayout();

                Thickness newSwitcherMargin = properties_hot_area.Margin;
                newSwitcherMargin.Left += dx;
                newSwitcherMargin.Right -= dx;
                newSwitcherMargin.Top += dy;
                newSwitcherMargin.Bottom -= dy;

                properties_hot_area.Margin = newSwitcherMargin;
                properties_hot_area.UpdateLayout();
                //properties_hot_area.SnapsToDevicePixels = true;

                updateLinkLinePosition();

                m_currentMousePosition = newMousePosition;

                m_isPropertyGridMoved = true;

                if (m_dataStore.isMoveAllPropertyWindow == true)
                {
                    if (m_delegateUpdatePositionOfAllPropertyWindow != null)
                        m_delegateUpdatePositionOfAllPropertyWindow(ID, newPropertyWindowMargin);
                }
            }
        }

        private void updateLinkLinePosition()
        {
            Thickness newLinkLineMargin = line_insideCircle.Margin;

            int xdirection = 0;
            int ydirection = 0;

            if (properties_hot_area.Margin.Left + 12 < main_grid.ActualWidth / 2)
            {
                xdirection = 1;
            }
            else
            {
                xdirection = 2;
            }

            if (properties_hot_area.Margin.Bottom + 12 > main_grid.ActualHeight / 2)
            {
                ydirection = 1;
            }
            else
            {
                ydirection = 2;
            }

            if (xdirection == 1 && ydirection == 1)
            {
                ScaleTransform sf = new ScaleTransform();
                sf.ScaleY = -1;
                sf.ScaleX = -1;
                line_insideCircle.RenderTransform = sf;
                linemask_insideCircle.RenderTransform = sf;
                line_insideCircle.UpdateLayout();
                linemask_insideCircle.UpdateLayout();

                newLinkLineMargin.Left = properties_hot_area.Margin.Left + 12;
                newLinkLineMargin.Right = main_grid.ActualWidth / 2;
                newLinkLineMargin.Top = properties_hot_area.Margin.Top + 22;
                newLinkLineMargin.Bottom = main_grid.ActualWidth / 2;
            }
            else if (xdirection == 2 && ydirection == 1)
            {
                ScaleTransform sf = new ScaleTransform();
                sf.ScaleY = -1;
                sf.ScaleX = 1;
                line_insideCircle.RenderTransform = sf;
                linemask_insideCircle.RenderTransform = sf;
                line_insideCircle.UpdateLayout();
                linemask_insideCircle.UpdateLayout();

                newLinkLineMargin.Left = main_grid.ActualWidth / 2;
                //margin3.Right = properties_hot_area.Margin.Right + 37;
                newLinkLineMargin.Right = main_grid.ActualWidth - properties_hot_area.Margin.Left - 11;
                newLinkLineMargin.Top = properties_hot_area.Margin.Top + 22;
                newLinkLineMargin.Bottom = main_grid.ActualWidth / 2;
            }
            else if (xdirection == 1 && ydirection == 2)
            {
                ScaleTransform sf = new ScaleTransform();
                sf.ScaleY = -1;
                sf.ScaleX = 1;
                line_insideCircle.RenderTransform = sf;
                linemask_insideCircle.RenderTransform = sf;
                line_insideCircle.UpdateLayout();
                linemask_insideCircle.UpdateLayout();

                newLinkLineMargin.Left = properties_hot_area.Margin.Left + 12;
                newLinkLineMargin.Right = main_grid.ActualWidth / 2;
                newLinkLineMargin.Top = main_grid.ActualWidth / 2;
                newLinkLineMargin.Bottom = properties_hot_area.Margin.Bottom + 12;
            }
            else
            {
                ScaleTransform sf = new ScaleTransform();
                sf.ScaleY = -1;
                sf.ScaleX = -1;
                line_insideCircle.RenderTransform = sf;
                linemask_insideCircle.RenderTransform = sf;
                line_insideCircle.UpdateLayout();
                linemask_insideCircle.UpdateLayout();

                newLinkLineMargin.Left = main_grid.ActualWidth / 2;
                //margin3.Right = properties_hot_area.Margin.Right + 37;
                newLinkLineMargin.Right = main_grid.ActualWidth - properties_hot_area.Margin.Left - 11;
                newLinkLineMargin.Top = main_grid.ActualWidth / 2;
                newLinkLineMargin.Bottom = properties_hot_area.Margin.Bottom + 12;
            }

            line_insideCircle.Margin = newLinkLineMargin;
            linemask_insideCircle.Margin = newLinkLineMargin;
            line_insideCircle.UpdateLayout();
            linemask_insideCircle.UpdateLayout();
        }

        private void Image_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (MSettingItem.Visibility == Visibility.Hidden)
            {
                MSettingItem.Visibility = Visibility.Visible;

                Canvas.SetZIndex(this, 999);

                updateSettings();
            }
            else
                MSettingItem.Visibility = Visibility.Hidden;
        }



        private void updateSettings()
        {
            if ( MSettingItem.Visibility == Visibility.Visible)
                MSettingItem.Margin = new Thickness(main_grid.ActualWidth/2.0, main_grid.ActualHeight-20, -MSettingItem.ActualWidth, -MSettingItem.ActualHeight);
        }

        private void mask_circle_MouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            main_menu.Visibility = Visibility.Visible;
            Point currentPos = e.GetPosition(main_grid);

            Thickness newMargin = new Thickness();
            newMargin.Left = currentPos.X;
            newMargin.Top = currentPos.Y;
            newMargin.Right = main_grid.ActualWidth - newMargin.Left - main_menu.ActualWidth;
            newMargin.Bottom = main_grid.ActualHeight - newMargin.Top - main_menu.ActualHeight;

            main_menu.Margin = newMargin;
        }

        private void main_menu_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            main_menu.Visibility = Visibility.Hidden;
        }
    }
}



